package org.rcgwt.client.ui.validator.impl;

import org.rcgwt.client.ui.validator.IChecker;
import org.rcgwt.client.ui.validator.IClientValidatorContext;
import org.rcgwt.client.ui.validator.ISingleton;
import org.rcgwt.client.ui.validator.IValidatorElement;
import org.rcgwt.client.ui.validator.messages.Severity;
import org.rcgwt.client.util.Pattern;

/**
 * A Checker which checks scientific form number (1.025E2)
 * 
 * @author Elian ORIOU
 * 
 */

public class ScientificChecker implements IChecker, ISingleton {

	private static final ScientificChecker INSTANCE = new ScientificChecker();

	private static final Pattern SCIENTIFIC_PATTERN = Pattern
			.compile("^([0-9]+)([.]?)([0-9]+)(E|e){1}([+]|[-]){1}([0-9]+)$");

	protected ScientificChecker() {

	}

	@Override
	public boolean check(IClientValidatorContext context, String s) {
		if (SCIENTIFIC_PATTERN.matches(s)) {
			return true;
		} else {
			context.addMessage(context.getMessagesProvider()
					.StructureErrorShort(), context.getMessagesProvider()
					.StructureError(), Severity.ERROR);
			return false;
		}
	}

	public static IValidatorElement getInstance() {
		return INSTANCE;
	}
}
